<?php
/**
 * Auth_model Class
 *
 */
class Lap_peternakan extends CI_Model {
	/**
	 * Constructor
	 */
	function Lap_peternakan()
		{
			parent::__construct();
		}
	
	function get_pertambahan_pengurangan(){
		return $this->db->select("*,sum(JUMLAH) as J")
						->group_by("MONTH(TANGGAL_KEJADIAN),YEAR(TANGGAL_KEJADIAN),JENIS_ALASAN")
						->order_by("TANGGAL_KEJADIAN")
						->get("v_pertambahan_pengurangan");
	}
	
	function get_pp_sum(){
		return $this->db->select("*,sum(JUMLAH) as J")
						->group_by("MONTH(TANGGAL_KEJADIAN),YEAR(TANGGAL_KEJADIAN),ID_KOTA")
						->order_by("TANGGAL_KEJADIAN")
						->get("v_pertambahan_pengurangan");
	}
	
	function get_distribusi_masuk(){
		return $this->db->select("*,sum(JUMLAH) as J")
						->from("v_distribusi_masuk")
						->join("peternakan", "peternakan.ID_TITIK_PASOK=v_distribusi_masuk.ID_TITIK_PASOK")
						->group_by("MONTH(TANGGAL_KEJADIAN),YEAR(TANGGAL_KEJADIAN), ID_KOTA")
						->get();
	}
	
	function get_distribusi_keluar(){
		return $this->db->select("*,sum(JUMLAH) as J")
						->from("v_distribusi_keluar")
						->join("peternakan", "peternakan.ID_TITIK_PASOK=v_distribusi_keluar.ID_TITIK_PASOK")
						->group_by("MONTH(TANGGAL_KEJADIAN),YEAR(TANGGAL_KEJADIAN), ID_KOTA")
						->get();
	}
	
	function get_max_pp(){
		$jumlah = $this->db->select("sum(JUMLAH) as J")
						->group_by("MONTH(TANGGAL_KEJADIAN),YEAR(TANGGAL_KEJADIAN)")
						->get("v_pertambahan_pengurangan");
		if($jumlah->num_rows()>0){
			return max($jumlah->result_array());
		}
	}
	
	function get_max_dist(){
		$jumlah	= $this->db->select("sum(JUMLAH) as J")
						->group_by("MONTH(TANGGAL_KEJADIAN),YEAR(TANGGAL_KEJADIAN)")
						->get("v_distribusi_keluar");
						
			
		if($jumlah->num_rows()>0){
			return max($jumlah->result_array());
		}
	}
	
	function get_kota(){
		return $this->db->select("distinct(ID_KOTA),NAMA_KOTA")
						->get("v_distribusi_masuk");
	}
	
	function hitung($count_max, $count_min, $hasil_hitung,$act="+"){
		foreach($count_max as $key=>$data){
			if(!isset($count_min[$key])){
				$count_min[$key] = 0;
			}
			if($act=="-"){
				$hasil_hitung[$key] = (int)$count_min[$key]-(int)$data;
			}else{
				$hasil_hitung[$key] = (int)$data-(int)$count_min[$key];
			}
		}
		foreach($count_min as $key=>$data){
			if(isset($hasil_hitung[$key])==0){
				$hasil_hitung[$key] = (int)$data;
			}
		}
		return $hasil_hitung;
	}
	
	function hasil_hitung($dist_masuk,$dist_keluar){
		$d_masuk 		= array();
		$d_keluar 		= array();
		$pp 			= array();
		$pp_sum			= $this->lap_peternakan->get_pp_sum();
		
		foreach($dist_masuk->result_array() as $data){
			$tgl = explode("-",$data["TANGGAL_KEJADIAN"]);
			$d_masuk[$tgl[0]."-".$tgl[1]."-01-".$data["ID_KOTA"]] = $data["J"];
		}
		
		foreach($dist_keluar->result_array() as $data){
			$tgl = explode("-",$data["TANGGAL_KEJADIAN"]);
			$d_keluar[$tgl[0]."-".$tgl[1]."-01-".$data["ID_KOTA"]] = $data["J"];
		}
		
		foreach($pp_sum->result_array() as $data){
			$tgl = explode("-",$data["TANGGAL_KEJADIAN"]);
			$pp[$tgl[0]."-".$tgl[1]."-01-".$data["ID_KOTA"]] = $data["J"];
		}
		
		$hasil_hitung = array();
		foreach($pp as $key=>$data){
			$hasil_hitung[$key] = 0;
		}
		foreach($d_masuk as $key=>$data){
			$hasil_hitung[$key] = 0;
		}
		foreach($d_keluar as $key=>$data){
			$hasil_hitung[$key] = 0;
		}
		
		
		if(count($d_masuk)>count($d_keluar)){
			$hasil_hitung = $this->hitung($d_masuk,$d_keluar,$hasil_hitung);
		}else{
			$hasil_hitung = $this->hitung($d_keluar,$d_masuk,$hasil_hitung,"-");
		}
		foreach($pp as $key=>$data){
			if(isset($hasil_hitung[$key])==0){
				$hasil_hitung[$key] = (int)$data;
			} else {
				 $hasil= $hasil_hitung[$key]+(int)$data;
				  $hasil_hitung[$key] = $hasil;
			}
		}
		return $hasil_hitung;
	}
	
	function hasil_fix($hasil_hitung){
		$kota 		= array();
		$hasil_fix 	= array();
		
		foreach($hasil_hitung as $key=>$data){
			$split_kota = explode("-", $key);
			
			if(!isset($kota[$split_kota[3]])){
				$kota[$split_kota[3]] = 0;
			}	
			$hasil_fix[$key] = $data+($kota[$split_kota[3]]);
			$kota[$split_kota[3]] = $hasil_fix[$key];
		}
		return $hasil_fix;
	}
	
	function mapping_hasil($hasil_fix){
		$mapping_hasil = array();
		foreach($hasil_fix as $key=>$data){
			$key_ex = explode("-", $key);
			$mapping_hasil[$key_ex[0]."-".$key_ex[1]."-".$key_ex[2]][$key_ex[3]] = $data;
		}
		ksort($mapping_hasil);
		return $mapping_hasil;
	}
}
// END Auth_model Class

/* End of file Auth_model.php */ 
/* Location: ./system/application/model/Auth_model.php */